package com.ltu.rest.wxlive;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ltu.config.initiallydata.InitiallyData;
import com.ltu.config.shiro.ShiroUtil;
import com.ltu.domain.mp_entity.PayRecord;
import com.ltu.domain.mp_entity.UserEntity;
import com.ltu.domain.mp_entity.customize.CompanyEntity;
import com.ltu.model.request.coupon.WxMaLiveCouponParentVO;
import com.ltu.rest.ThirdPartyHeader;
import com.ltu.service.customize.CompanyService;
import com.ltu.util.SpringUtil;
import com.wechat.pay.java.core.RSAAutoCertificateConfig;
import com.wechat.pay.java.service.merchantexclusivecoupon.MerchantExclusiveCouponService;
import com.wechat.pay.java.service.merchantexclusivecoupon.model.CouponStatus;
import com.wechat.pay.java.service.merchantexclusivecoupon.model.CreateBusiFavorStockRequest;
import com.wechat.pay.java.service.merchantexclusivecoupon.model.CreateBusiFavorStockResponse;
import com.wechat.pay.java.service.merchantexclusivecoupon.model.ListCouponsByFilterRequest;
import com.wechat.pay.java.service.merchantexclusivecoupon.model.UseCouponRequest;
import com.wechat.pay.java.service.merchantexclusivecoupon.model.UseCouponResponse;

import cn.binarywang.wx.miniapp.api.WxMaService;
import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import me.chanjar.weixin.common.error.WxErrorException;

@Slf4j
@Component
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class WxMaLiveRestServiceImpl  implements WxMaLiveRestService,ThirdPartyHeader{
//	private final RedistUtil redistUtil;
//	private final RestTemplate restTemplate;
//	private final SysDict.ThirdPartyDict dict = SysDict.ThirdPartyDict.WxMaLive;
	private final CompanyService companyService;
	private final WxMaService wxMaService;

	
	//设置请求头
	@Override
	public  void setRestHeader(HttpRequest request) {
//		Map<String,String> headers = new HashMap<String, String>(4);
//		request.getHeaders().add("Content-Type", "application/x-www-form-urlencoded");
		request.getHeaders().add("Content-Type", "application/json");
	
	}
	

	

	
	
	private static  String  WxSendOrderUrl = "https://api.weixin.qq.com/wxa/sec/order/upload_shipping_info";
	/**
	 * 小程序订单发货
	 * @throws WxErrorException 
	 */
	@Override
	public String sendOrder(PayRecord  payRecord,CompanyEntity   company) throws WxErrorException {
//		JSONObject json =	(JSONObject) JSONObject.toJSON(wxMaLiveCouponParentVO);
//		
//		log.info("请求创建商家券批次号的参数：{}",json.toString());
//		CreateBusiFavorStockRequest param = json.toJavaObject(CreateBusiFavorStockRequest.class);
//		RSAAutoCertificateConfig  config =	 SpringUtil.getBean(InitiallyData.RSAAutoCertificateKey.concat(company.getMchId()), RSAAutoCertificateConfig.class);
//		MerchantExclusiveCouponService wxMchCouponService = new MerchantExclusiveCouponService.Builder().config(config).build();
//		CreateBusiFavorStockResponse respon = wxMchCouponService.createBusifavorStock(param);
//		
//		return respon;
		
		Date curDate = new Date();
		JSONObject  param = new JSONObject();
		JSONObject  order_key= new JSONObject();
		order_key.put("order_number_type", 1);
		order_key.put("out_trade_no", payRecord.getOrderNo());
		order_key.put("mchid", company.getMchId());
		param.put("order_key", order_key);
		param.put("logistics_type", 4);
		param.put("delivery_mode", 1);
		param.put("upload_time",  DateUtil.format(curDate, DatePattern.NORM_DATETIME_FORMAT).replace(" ", "T")+"+08:00");
		JSONObject  payer = new JSONObject();
		payer.put("openid", payRecord.getOpenId());
		param.put("payer", payer );
		
		JSONObject  shipping_list = new JSONObject();
		shipping_list.put("item_desc", payRecord.getPayName());
		com.alibaba.fastjson.JSONArray  arr = new JSONArray(1);
		arr.add(shipping_list);
		param.put("shipping_list",  arr);
		wxMaService.switchoverTo(company.getAppId());
		
		String  resp =  wxMaService.post( WxSendOrderUrl, param.toJSONString());
		log.info("微信小程序确认收货结果："+ resp);
		return resp;
		
		
	}

	
	
}
